package com.neuedu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neuedu.common.Result;
import com.neuedu.pojo.CateParams;
import com.neuedu.service.CateParamsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/cateparams")
@Slf4j
public class CateParamsController {

    @Autowired
    private CateParamsService cateParamsService;

    /**
     * 根据分类id获取参数集合
     *
     * @param categoryId
     * @param type
     * @return
     */
    @GetMapping
    public Result<List<CateParams>> getCateParamsList(Long categoryId, Integer type) {
        LambdaQueryWrapper<CateParams> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CateParams::getType, type);
        queryWrapper.eq(CateParams::getCategoryId, categoryId);
        queryWrapper.orderByDesc(CateParams::getSort);
        List<CateParams> paramsList = cateParamsService.list(queryWrapper);
        return Result.success(paramsList);
    }

    /**
     * 根据id获取参数信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<CateParams> getCateParamsById(@PathVariable Long id) {
        CateParams cateParams = cateParamsService.getById(id);
        return Result.success(cateParams);
    }


    /**
     * 新增分类参数信息（未添加商品属性值）
     *
     * @param cateParams
     * @return
     */
    @PostMapping
    public Result<String> saveCateParams(@RequestBody CateParams cateParams) {
        if (cateParams.getSort() == null) {
            cateParams.setSort(0);
        }
        cateParamsService.save(cateParams);
        return Result.success("添加参数信息成功");
    }

    /**
     * 修改商品参数信息
     *
     * @param cateParams
     * @return
     */
    @PutMapping
    public Result<String> update(@RequestBody CateParams cateParams) {
        cateParamsService.updateById(cateParams);
        return Result.success("修改参数信息成功");
    }

    /**
     * 删除分类参数信息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<String> update(@PathVariable Long id) {
        cateParamsService.removeById(id);
        return Result.success("删除参数信息成功");
    }


    /**
     * 新增分类参数属性值
     *
     * @param id
     * @param map
     * @return
     */
    @PostMapping("/attributes/{id}")
    public Result<String> saveCateParamsVals(@PathVariable Long id, @RequestBody Map<String, String> map) {
        String paramVal = map.get("paramVal");
        CateParams cateParams = cateParamsService.getById(id);
        String paramVals = cateParams.getParamVals();
        if (paramVals == null || paramVals.length() == 0) {
            paramVals = paramVal;
        } else if (paramVals.contains(paramVal)) {
            return Result.error(paramVal + " 已存在");
        } else {
            paramVals = paramVals + "," + paramVal;
        }
        cateParams.setParamVals(paramVals);
        cateParamsService.updateById(cateParams);
        return Result.success("添加分类参数属性值成功");
    }

//    /**
//     * 修改分类参数属性值
//     *
//     * @param id
//     * @param map
//     * @return
//     */
//    @PutMapping("/attributes/{id}")
//    public Result<String> updateCateParamsVals(@PathVariable Long id, @RequestBody Map<String, String> map) {
//        String paramVal = map.get("paramVal");
//        int index = Integer.parseInt(map.get("index"));
//        CateParams cateParams = cateParamsService.getById(id);
//        String paramVals = cateParams.getParamVals();
//        String[] paramValsTmp = paramVals.split(",");
//        paramValsTmp[index] = paramVal;
//        paramVals = StringUtils.join(paramValsTmp, ",");
//        cateParams.setParamVals(paramVals);
//        cateParamsService.updateById(cateParams);
//        return Result.success("修改分类参数属性值成功");
//    }

    /**
     * 删除分类参数属性值
     *
     * @param id
     * @param paramVal
     * @return
     */
    @DeleteMapping("/attributes/{id}")
    public Result<String> deleteCateParamsVals(@PathVariable Long id, String paramVal) {

        CateParams cateParams = cateParamsService.getById(id);
        String paramVals = cateParams.getParamVals();
        List<String> attributes = new ArrayList<>(Arrays.asList(paramVals.split(",")));
        attributes.remove(paramVal);
        paramVals = StringUtils.join(attributes, ",");
        cateParams.setParamVals(paramVals);
        cateParamsService.updateById(cateParams);
        return Result.success("删除分类参数属性值成功");
    }

    /**
     * 获取分类参数信息
     *
     * @param id
     * @return
     */
    @GetMapping("/attributes/{id}")
    public Result<String> getCateParamsVals(@PathVariable Long id) {
        CateParams cateParams = cateParamsService.getById(id);
        String paramVals = cateParams.getParamVals();
        return Result.success(paramVals);
    }
}
